<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    * {
      margin: 0;
      padding: 0;
      list-style: none;
      transition:background-color 0.5s linear;
    }
    body {
      background-color: #0b0a319f;
    }
    #app {
      width: 50px;
      height: 100px;
    }
  </style>
</head>
<body>
  <div id="app">

  </div>
  <script>
    function Color(ele) {
      this.ele = ele
      this.changeColor = function () {
        let color_list = ['#2ecf71','#9b59b6','#2c3e50','#e67e22','#e74c3c','#16a085','#fd79a8',]
        setInterval(function () {
          let i = Math.floor(Math.random()*color_list.length)
          this.ele.style.backgroundColor = color_list[i]
        }.bind(this),1000) // bind 方法不会立即执行  而 call  apply都会立即执行
      }
    }
    let a = new Color(document.body)
    let b = new Color(document.querySelector('#app'))
    a.changeColor()
    b.changeColor()
  </script>
</body>
</html>